package de.carcophan.jimdb.gwt.server.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.enterprise.context.ApplicationScoped;

import org.apache.commons.codec.binary.Base64;
import org.jboss.resteasy.util.Hex;

@ApplicationScoped
public class PasswordUtil {

   public String getHash(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
      MessageDigest md = MessageDigest.getInstance("SHA-256");
      md.update(password.getBytes("UTF-8"));
      byte digest[] = md.digest();
      String hash = Hex.encodeHex(digest);
      byte[] encodeBase64 = Base64.encodeBase64(hash.getBytes());
      hash = new String(encodeBase64);
      return hash;
   }

}
